package com.velvetantler.weather.view.home

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.qweather.sdk.Callback
import com.qweather.sdk.QWeather
import com.qweather.sdk.parameter.weather.WeatherParameter
import com.qweather.sdk.response.error.ErrorResponse
import com.qweather.sdk.response.weather.WeatherHourlyResponse
import com.qweather.sdk.response.weather.WeatherNowResponse
import com.velvetantler.weather.R
import com.velvetantler.weather.base.BaseInteractor
import com.velvetantler.weather.bean.CityInfo
import com.velvetantler.weather.utils.WeatherUtils

class HomeModel : BaseInteractor<HomeView>() {

    /**
     * 请求获取实时天气数据
     */
    fun requestWeatherNowData(
        longitude: String,
        latitude: String,
        onSucceed: (WeatherNowResponse) -> Unit,
        onFailed: (String) -> Unit
    ) {
        // 格式化经纬度
        val longitudeFormat = WeatherUtils.formatLatLng(longitude)
        val latitudeFormat = WeatherUtils.formatLatLng(latitude)
        val location = "$longitudeFormat,$latitudeFormat"
        // 请求实时天气
        QWeather.instance.weatherNow(
            WeatherParameter(location),
            object : Callback<WeatherNowResponse> {
                override fun onSuccess(response: WeatherNowResponse?) {
                    Handler(Looper.getMainLooper()).post {
                        onSucceed(response!!)
                    }
                }

                override fun onFailure(errorResponse: ErrorResponse?) {
                    Handler(Looper.getMainLooper()).post {
                        onFailed(errorResponse!!.toString())
                    }
                }

                override fun onException(e: Throwable?) {
                    Handler(Looper.getMainLooper()).post {
                        onFailed(e!!.toString())
                    }
                }

            })
    }

    /**
     * 获取逐小时天气数据
     */
    fun requestWeatherHourlyData(
        longitude: String,
        latitude: String,
        onSucceed: (WeatherHourlyResponse) -> Unit,
        onFailed: (String) -> Unit
    ) {
        // 格式化经纬度
        val longitudeFormat = WeatherUtils.formatLatLng(longitude)
        val latitudeFormat = WeatherUtils.formatLatLng(latitude)
        val location = "$longitudeFormat,$latitudeFormat"
        // 获取逐小时天气数据
        QWeather.instance.weather24h(
            WeatherParameter(location),
            object : Callback<WeatherHourlyResponse> {
                override fun onSuccess(response: WeatherHourlyResponse?) {
                    Handler(Looper.getMainLooper()).post {
                        onSucceed(response!!)
                    }
                }

                override fun onFailure(errorResponse: ErrorResponse?) {
                    Handler(Looper.getMainLooper()).post {
                        onFailed(errorResponse!!.toString())
                    }
                }

                override fun onException(p0: Throwable?) {
                    onFailed(p0!!.toString())
                }
            }
        )
    }

    /**
     * 获取城市列表数据
     */
    fun requestCityListData(
        context: Context,
        onSucceed: (List<CityInfo>) -> Unit,
        onFailed: (String) -> Unit
    ) {
        val inputStream = context.resources.openRawResource(R.raw.city_list)
        val cityList: List<CityInfo> = Gson().fromJson(
            inputStream.bufferedReader().use { it.readText() },
            object : TypeToken<List<CityInfo>>() {}.type
        )
        if (cityList.isNotEmpty()) {
            onSucceed(cityList)
        } else {
            onFailed("城市列表为空")
        }
    }
}
